package com.dg.gaming.server.data.mysql;

import java.io.*;
import java.sql.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2009</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class DBGame extends ADBObject {
    private static final String SQL_LOAD_FULL = "select game_id,game_name ,game_descr, min_players, max_players,main_class_name,data,changed,modelclassname,deleyinsec,loseaftersecond from  game where game_id=?";
    private static final String SQL_LOAD_SIMPLEL =
            "select game_id,game_name,main_class_name,data,deleyinsec,loseaftersecond from  gamewhere game_id=?";
    private static final String SQL_SAVE =
            "UPDATE game set data=? where game_id=?";

    public static final PrepareStatmentObject PSO_LOAD_FULL = new
            PrepareStatmentObject(SQL_LOAD_FULL);
    public static final PrepareStatmentObject PSO_LOAD_SIMPLE = new
            PrepareStatmentObject(SQL_LOAD_SIMPLEL);
    public static final PrepareStatmentObject PSO_SAVE = new
            PrepareStatmentObject(SQL_SAVE);


    protected void loadFull(ResultSet rs) throws Exception {
        loadSimple(rs);
        setMin_players(rs.getInt("min_players"));
        setMax_players(rs.getInt("min_players"));
        setGame_descr(rs.getString("game_descr"));
        setModelclassname(rs.getString("modelclassname"));

    }


    protected void loadSimple(ResultSet rs) throws Exception {
        setGame_id(rs.getLong("game_id"));
        setGame_name(rs.getString("game_name"));
        setMain_class_name(rs.getString("main_class_name"));
        setDeleyinsec(rs.getInt("deleyinsec"));
        setLoseaftersecond(rs.getInt("loseaftersecond"));

        java.io.InputStream is = rs.getBinaryStream("data");

        setData(DBManager.loadBytes(is));

    }

    protected boolean loadSimple(long gameID, Connection con) throws Exception {
        PreparedStatement ps = PSO_LOAD_FULL.getPrepareStmt(con);
        ResultSet r = null;
        try {
            ps.setLong(1, gameID); ;
            r = ps.executeQuery();
            if (r.next()) {
                loadSimple(r);
                return true;
            } else {
                return false;
            }

        } finally {
            if (r != null) {
                r.close();
            }
            if (ps!=null) {
                ps.close();
            }

        }

    }

    protected boolean loadFull(long gameID, Connection con) throws Exception {
        PreparedStatement ps = PSO_LOAD_FULL.getPrepareStmt(con);
        ResultSet r = null;
        try {
            ps.setLong(1, gameID); ;
            r = ps.executeQuery();
            if (r.next()) {
                loadFull(r);
                return true;
            } else {
                return false;
            }

        } finally {
            if (r != null) {
                r.close();
            }
            if (ps!=null) {
                ps.close();
            }

        }
    }


    private long game_id;
    private String game_name;
    private String game_descr;
    private int min_players;
    private int max_players;
    private String main_class_name;
    private byte[] data;
    private boolean changed;
    private String modelclassname;
    private int deleyinsec;
    private int loseaftersecond;
    public DBGame() {
    }


    protected void save(PreparedStatement ps) throws Exception {

        InputStream bodyIn = new ByteArrayInputStream(data);
        ps.setBinaryStream(1, bodyIn, data.length);
        ps.setLong(2, getGame_id());
        bodyIn.close();
    }

    public boolean isChanged() {
        return changed;
    }

    public byte[] getData() {
        return data;
    }

    public String getGame_descr() {
        return game_descr;
    }

    public long getGame_id() {
        return game_id;
    }

    public String getGame_name() {
        return game_name;
    }

    public String getMain_class_name() {
        return main_class_name;
    }

    public int getMax_players() {
        return max_players;
    }

    public int getMin_players() {
        return min_players;
    }

    public String getModelclassname() {
        return modelclassname;
    }

    public int getDeleyinsec() {
        return deleyinsec;
    }

    public int getLoseaftersecond() {
        return loseaftersecond;
    }

    public void setChanged(boolean changed) {
        this.changed = changed;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

    public void setGame_descr(String game_descr) {
        this.game_descr = game_descr;
    }

    public void setGame_id(long game_id) {
        this.game_id = game_id;
    }

    public void setGame_name(String game_name) {
        this.game_name = game_name;
    }

    public void setMain_class_name(String main_class_name) {
        this.main_class_name = main_class_name;
    }

    public void setMax_players(int max_players) {
        this.max_players = max_players;
    }

    public void setMin_players(int min_players) {
        this.min_players = min_players;
    }

    public void setModelclassname(String modelclassname) {
        this.modelclassname = modelclassname;
    }

    public void setDeleyinsec(int deleyinsec) {
        this.deleyinsec = deleyinsec;
    }

    public void setLoseaftersecond(int loseaftersecond) {
        this.loseaftersecond = loseaftersecond;
    }


}
